package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import model.Comment;
import model.User;

import org.apache.log4j.Logger;

import util.ParameterCheck;

import common.Constant;
import common.StartupServlet;

public class CommentDao extends RpDao {
	private static Logger log = Logger.getLogger(CommentDao.class);

	public int CreateComment(Comment comment) {
		int n = 0;
		Connection conn=this.getConn();
		
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			//			insertComment="insert into comment(itemid,uid,content,vote) values(?,?,?,?)",
			pstmt =conn.prepareStatement(SqlString.insertComment, Statement.RETURN_GENERATED_KEYS);
			pstmt.setInt(1, comment.getItemid());
			pstmt.setInt(2, comment.getUid());
			pstmt.setString(3, comment.getContent());
			pstmt.setInt(4, comment.getVote());

			n = pstmt.executeUpdate();
			if (1 == n) {
				rs = pstmt.getGeneratedKeys();
				rs.next();
				n = rs.getInt(1);// 成功则返回 id
			}
		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e.getMessage());
			n = -1;
		} finally {
			this.closeDao(conn,pstmt, rs);
		}
		return n;
	}

	public boolean updateVote(int id) {
		boolean res = false;
		Connection conn=this.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			//updateComment = "update comment set vote=vote+1 where id=?",
			pstmt = conn.prepareStatement(SqlString.updateComment, Statement.RETURN_GENERATED_KEYS);
			pstmt.setInt(1, id);
			id = pstmt.executeUpdate();
			if (id > 0) {
				res = true;
			} else {
				res = false;
			}
		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		} finally {
			this.closeDao(conn,pstmt, rs);
		}

		return res;
	}

	/**
	 * 按item分页获取comment
	 * @param itemid
	 * @param page
	 * @return
	 */
//	public List<Comment> queryCommentByItemid(int itemid, int page) {
//		return this.getComments(SqlString.selectCommentByItemId, itemid, page);
//	}


	/**
	 * 获取item前10个comment
	 * @param itemid
	 * @return
	 */
//	public List<Comment> queryComment(int itemid,int page) {
//		return queryCommentByItemid(itemid, 0);
//	}

	public List<Comment> getComments(int id, int page) {
		
		List<Comment> res = new ArrayList<Comment>();
		Connection conn=this.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;

		int from = 0;
		if (page >= 1) {
			from = (page - 1) * Constant.PageSize ;
		}
		try {
			pstmt = conn.prepareStatement(SqlString.selectCommentByItemId);
			pstmt.setInt(1, id);
			pstmt.setInt(2, from);
			pstmt.setInt(3, Constant.PageSize);
			rs = pstmt.executeQuery();

			while (rs.next()) {
				Comment cm = new Comment();
				cm.setId(rs.getInt("id"));
				cm.setUid(rs.getInt("uid"));
				cm.setItemid(rs.getInt("itemid"));
				cm.setContent(rs.getString("content"));
				cm.setCtime(rs.getString("ctime"));
				cm.setVote(rs.getInt("vote"));
				cm.setDel(rs.getInt("del"));
				cm.setUname(rs.getString("name"));
				res.add(cm);
			}
			
			
//			获取投票数据
//			selectVoteInfo="select * from comment where type=?",
			UserDao userdao=new UserDao();
			for(Comment cm:res){
				rs=conn.createStatement().executeQuery(SqlString.selectVoteInfo+cm.getId());
				while(rs.next()){
					User user=userdao.getUser(rs.getInt("uid"));
					if(null==user)
						continue;
					cm.addVoteUser(user.getName());
					cm.addVoteTime(rs.getString("ctime"));
				}
			}		

		} catch (SQLException e) {
			e.printStackTrace();
			log.error(e.getMessage());
		} finally {
			this.closeDao(conn,pstmt, rs);
		}
		return res;
	}

	/**
	 * return comment times of some user
	 * @param uid
	 * @return
	 */
	public int getCommentTime(int uid,int itemid) {
		// TODO Auto-generated method stub
		int res = -1;
		Connection conn=this.getConn();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
//		CommentTime = "select count(*) from comment where uid=? and itemid=?",
			pstmt = conn.prepareStatement(SqlString.CommentTime);
			pstmt.setInt(1, uid);
			pstmt.setInt(2, itemid);
			rs = pstmt.executeQuery();
			if (rs.next()) {
				res = rs.getInt(1);
			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			this.closeDao(conn,pstmt, rs);
		}
		return res;
	}

	
	public int getCommentCount(int itemid){
		int n=0;
		Connection conn=this.getConn();
		ResultSet rs=null;
		try {
//			selectCommentCount="select count(*) from comment where del=0 and type=0 and itemid=",
			rs=conn.createStatement().executeQuery(SqlString.selectCommentCount+itemid);
			
			if(rs.next())
				n=rs.getInt(1);
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			this.closeDao(conn, null, rs);
		}
		
		return n;
	}
	
	public static void main(String[] args) {

		new StartupServlet();
		Comment cm = new Comment();
		cm.setContent("haha");
		cm.setItemid(1);
		cm.setUid(1);
		//		cm.setVote(1);
//		CommentDao cmdao = new CommentDao();

		System.out.println("new commentid:" + new CommentDao().CreateComment(cm));

		//		List<Comment> ls = cmdao.queryCommentByItemid(1, 0);
//		List<Comment> ls = cmdao.queryCommentByUid(3, 0);
//		for (Comment c : ls) {
//			System.out.println(c.getContent());
//			System.out.println(c.getItemid());
//			System.out.println(c.getId());
//			System.out.println(c.getUid());
//			System.out.println(c.getCtime());
//		}
//		System.out.println(cmdao.getCommentTime(3));
	}
}
